<?php

declare(strict_types=1);

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Exception\BusinessException;
use App\Model\ChargeOption;
use App\Model\Device;
use App\Model\Order;
use App\Services\AdapayService;
use App\Services\Bolai\BKV;
use App\Services\Bolai\BlService;
use App\Services\Bolai\PackService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Redis\Redis;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;

class TestController extends AbstractController
{

    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[inject]
    protected BlService $blService;

    #[inject]
    protected PackService $packService;

    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);

    }

    public function selectParam(RequestInterface $request)
    {
        $mac = $request->input('mac');
        $device = Device::query()->where('mac', $mac)->first();
        $res = $this->packService->selectDeviceParams($device);
        $data['base64'] = base64_encode($res);
        $data['hex'] = bin2hex($res);
        $data['bkv'] = substr($data['hex'], 8, strlen($data['hex']) - 14);
        return $this->success($data);
    }

    public function selectStatus(RequestInterface $request)
    {
        $mac = $request->input('mac');
        $device = Device::query()->where('mac', $mac)->first();
        $res = $this->packService->selectDeviceStatus($device);
        $data['base64'] = base64_encode($res);
        $data['hex'] = bin2hex($res);
        $data['bkv'] = substr($data['hex'], 8, strlen($data['hex']) - 14);
        return $this->success($data);
    }

    public function test(RequestInterface $request)
    {
        $mac = $request->input('mac');
        $fd = Device::where('mac', $mac)->value('fd');
        $report_cycle = $request->input('report_cycle', 0);
        $heart_cycle = $request->input('heart_cycle', 0);
        $full_charge_delay = $request->input('full_charge_delay', 0);
        $null_charege_delay = $request->input('null_charege_delay', 0);
        $full_charge_power = $request->input('full_charge_power', 0);
        $null_charge_power = $request->input('null_charge_power', 0);
        $temp_th = $request->input('temp_th', 0);
        $max_charge_time = $request->input('max_charge_time', 0);
        $threshold_i = $request->input('threshold_i', 0);
        $threshold_p = $request->input('threshold_p', 0);
        $charge_single_amount = $request->input('charge_single_amount', 0);
        $res = $this->packService->setDeviceParams($fd, $mac, $report_cycle, $heart_cycle, $full_charge_delay, $null_charege_delay, $full_charge_power, $null_charge_power, $temp_th, $max_charge_time, $threshold_i, $threshold_p, $charge_single_amount);
        $data['base64'] = base64_encode($res);
        $data['hex'] = bin2hex($res);
        $data['bkv'] = substr($data['hex'], 8, strlen($data['hex']) - 14);
        return $this->success($data);
//        $data= '040101100c0a0102008e010058b300000a0103100802404290014903010f0104011e003c040120003c0401211c20040122002d04012300640401240008030125550401102ee004011155f00301600a040159025806016800000064';
//        $b = new BKV();
//        $upr = BKV::unPack($b->hexToBuffer($data));
//        if ($upr['code'] !== 0) {
//            throw new BusinessException(ErrorCode::BKV_UNPACK_FAIL);
//        }
//        var_dump($upr['bkv']->dump());
    }

    public function test1()
    {
//        $this->blService->saveHeartHour(1,23);
    }

    public function system_restart(RequestInterface $request)
    {
        $mac = $request->input('mac');
        $fd = Device::where('mac', $mac)->value('fd');
        $min = $request->input('min', 0);
        $res = $this->packService->system_restart($fd, $mac, $min);
        $data['base64'] = base64_encode($res);
        $data['hex'] = bin2hex($res);
        $data['bkv'] = substr($data['hex'], 8, strlen($data['hex']) - 14);
        return $this->success($data);
    }


    public function testTcp()
    {
        $result = $this->blService->sendToDevice(1, 'test message');
        $data['msg'] = $result;
        return $this->success($data);
    }


    public function endOrder(RequestInterface $request){
        $id  = $request->input('id');
        $order = Order::where('id',$id)
            ->first();
        $order->stop('结束并结算',true);
    }

    public function testBkv(RequestInterface $request)
    {
        $order = Order::where('id', 1)->first();
        $params['pay_time'] = date('YmdHis');
        $order->payMent($params);
//        $mac = $request->input('mac');
//        $port = $request->input('port',0);
//        $device = Device::where('mac',$mac)->first();
//        $charge = ChargeOption::where('id',$device->charge_id)
//            ->with('power_children')->first();
//        $res = $this->packService->startPowerPort($device,$port,4,$charge);
//        $data['base64'] = base64_encode($res);
//        $data['hex'] = bin2hex($res);
//        return $this->success($data);

//         $bkvData = '040101100c0a010200000000000000000a0103105102408060047903010f0104011e003c040120003c0401211c20040122002d04012300640401240008030125550401102cec04011156540301600a0401590258';
//        $b = new BKV();
//        $upr = BKV::unPack($b->hexToBuffer($bkvData));
//        if ($upr['code'] !== 0) {
//            throw new BusinessException(ErrorCode::BKV_UNPACK_FAIL);
//        }
//        $message = $upr['bkv']->dump(); // 打印解包后的键值对
//        var_dump($message);


//        $b = new BKV();
//        $b->addByNumberKey(0x01, 0x1001);
//        $b->addByNumberKey(0x02, $this->blService->generateRequestId());
//        $b->addByNumberKey(0x03, intval('1051024080600479'));
//        $b->addByNumberKey(0x06, $b->getBcdTime());
//        $result = $b->pack();
//        echo $b->bufferToHex($result);

//        $base64 = '/P8AOAQBARABEgECMmE3MTQyMWQxNTU2YzQ1MBIBAzEwNTEwMjQwODA2MDA0NzkJAQYgJQEIF1cSSvzu';
//        $result =  $this->blService->parse($base64)->handle(4);
//        return $this->success($result);
    }

}
